#include "linearList.h"

using namespace std;
//顺序表打印
void sequenceList::print() {
    cout << curNumberOfItem << ":";

    for(int i = 0; i < curNumberOfItem-1; i++) {
        cout << myList[i] << ",";
    }

    cout << myList[curNumberOfItem-1] << endl;
}



//链表打印
void linkList::print() {
    curNode = firstNode;

    cout << listSize << ":";

    while(curNode != lastNode) {
        if(curNode->next == lastNode)
            cout << curNode->next->data << endl;
        else {
            cout << curNode->next->data << ",";
        }


        curNode = curNode->next;
    }
}
//listNode构造函数
listNode::listNode(float nodeData, listNode* succ) {
    this->data = nodeData;
    this->next = succ;
}
//listNode析构函数
listNode::~listNode() {
    this->data = 0;
    this->next = NULL;
}
//linkList默认构造函数
linkList::linkList() {
    this->firstNode=new listNode();
    this->curNode=NULL;
    this->lastNode=NULL;
    this->listSize=0;
}
//linkList构造函数
linkList::linkList(const int& length, float arr[]) {
    this->firstNode=new listNode();
    this->curNode=this->firstNode;
    this->listSize=length;
    for(int i=0; i<length; i++) {
        this->curNode->next=new listNode();
        this->curNode=this->curNode->next;
        this->curNode->data=arr[i];
    }
    this->lastNode=this->curNode;
}
//linkList析构函数
linkList::~linkList() {
    this->curNode=this->firstNode;
    this->firstNode=NULL;
    listNode* temp;
    for(int i=0; i<this->listSize+1; i++) {
        temp=this->curNode->next;
        if(this->curNode!=NULL) {
            delete this->curNode;
            this->curNode=temp;
        }
    }
    lastNode=NULL;
}
//linkList尾插函数
bool linkList::tailInsertItem(const float& num) {
    listNode* temp=new listNode(num);
    if(temp==NULL)return false;
    this->lastNode->next=temp;
    this->lastNode=temp;
    if(this->listSize==0)this->firstNode=temp;
    this->listSize++;
    return true;
}
//linkList头插函数
bool linkList::headInsertItem(const float& num) {
    listNode* temp=new listNode(num,this->firstNode->next);
    if(temp==NULL)return false;
    if(this->listSize==0)this->lastNode=temp;
    this->firstNode->next=temp;
    this->listSize++;
    return true;
}
//linkList删除函数
int linkList::deleteItem(const float& num) {
    this->curNode=firstNode;
    for(int i=0; i<this->listSize-1; i++) {
        if(this->curNode->next->data==num) {
            listNode*temp=this->curNode->next->next;
            delete this->curNode->next;
            this->curNode->next=temp;
            this->listSize--;
            return i;
        } else this->curNode=this->curNode->next;
    }
    if(this->lastNode->data==num) {
        delete this->lastNode;
        this->lastNode=this->curNode;
        this->curNode->next=NULL;
        this->listSize--;
        return listSize;
    }
    return -1;
}
//linkList定位拷贝函数
bool linkList::locate(const int& loc, float& val) {
    if(loc>=this->listSize)return false;
    this->curNode=this->firstNode;
    for(int i=0; i<=loc; i++)this->curNode=this->curNode->next;
    val=this->curNode->data;
    return true;
}
//linkList定位函数
int linkList::locate(const float& num) {
    this->curNode=firstNode;
    for(int i=0; i<this->listSize; i++) {
        if(this->curNode->next->data==num) return i;
        else this->curNode=this->curNode->next;
    }
    return -1;
}
//linkList逆序函数
void linkList::reverse() {
    this->lastNode=this->firstNode->next;
    this->curNode=this->firstNode->next;
    listNode* temp2=curNode->next;
    while(temp2!=NULL) {
        listNode* temp1=this->curNode;
        this->curNode=temp2;
        temp2=this->curNode->next;
        this->curNode->next=temp1;
    }
    this->lastNode->next=NULL;
    this->firstNode->next=curNode;
}
//linkList插入函数
int linkList::insertItem(const int& loc,const float& num) {
    if(loc>this->listSize)return -1;
    if(loc==this->listSize)this->tailInsertItem(num);
    else {
        this->curNode=this->firstNode;
        for(int i=0; i<loc; i++)this->curNode=this->curNode->next;
        listNode* temp=new listNode(num,this->curNode->next);
        this->curNode->next=temp;
        this->listSize++;
    }
    return loc;
}
//linkList升序函数
void linkList::ascendingOrder() {
    linkList* new_link=new linkList();
    this->curNode=this->firstNode;
    for(int i=0; i<this->listSize; i++) {
        this->curNode=this->curNode->next;
        float x=this->curNode->data;
        float y;
        if(new_link->listSize==0) {
            new_link->headInsertItem(x);
            continue;
        }
        for(int j=0; j<new_link->listSize; j++) {
            new_link->locate(j,y);
            if(x<=y) {
                new_link->insertItem(j,x);
                break;
            } else if(j==new_link->listSize-1) {
                new_link->tailInsertItem(x);
                break;
            }
        }
    }
    listNode* temp=this->firstNode;
    this->firstNode=new_link->firstNode;
    this->lastNode=new_link->lastNode;
    delete temp;
}
//linkList连接函数
void linkList::add(linkList& B) {
    this->listSize=this->listSize+B.listSize;
    this->lastNode->next=B.firstNode->next;
    this->lastNode=B.lastNode;
}
//linkList合并函数
void merge(linkList& A, linkList& B) {
    A.add(B);
    A.ascendingOrder();
    A.reverse();
}
//sequenceList构造函数
sequenceList::sequenceList(const int& size, const int& length, float arr[]) {
    myList=new float[size];
    maxCapcity=size;
    curNumberOfItem =length;
    for(int i=0; i<length; i++)myList[i]=arr[i];
}
//sequenceList析构函数
sequenceList::~sequenceList() {
    delete  []myList;
}
//sequenceList插入函数
bool sequenceList::insertItem(const int& pos,const float& num) {
    if(maxCapcity==curNumberOfItem)return false;
    for(int i=curNumberOfItem; i>pos; i--)myList[i]=myList[i-1];
    myList[pos]=num;
    curNumberOfItem++;
    return true;
}
//sequenceList删除函数
int sequenceList::deleteItem(const float& num) {
    for(int i=0; i<curNumberOfItem; i++)if(myList[i]==num) {
            for(int j=i; j<curNumberOfItem-1; j++)myList[j]=myList[j+1];
            curNumberOfItem--;
            return i;
        }
    return -1;
}
//sequenceList定位拷贝函数
bool sequenceList::locate(const int& pos, float& num) {
    if(pos>=curNumberOfItem)return false;
    num=myList[pos];
    return true;
}
//sequenceList定位函数
int sequenceList::locate(const float& num) {
    for(int i=0; i<curNumberOfItem; i++)if(myList[i]==num)return i;
    return -1;
}
//sequenceList交换函数
void sequenceList::swap(int pos) {
float temp=myList[pos];
myList[pos]=myList[curNumberOfItem-pos-1];
myList[curNumberOfItem-pos-1]=temp;
}
//sequenceList逆序函数
void sequenceList::reverse() {
int times=curNumberOfItem%2?(curNumberOfItem-1)/2:curNumberOfItem/2;
for(int i=0;i<times;i++)this->swap(i);
}

bool sequenceList::addItem(const float& num) {
if(this->curNumberOfItem==this->maxCapcity)return false;
this->myList[curNumberOfItem]=num;
this->curNumberOfItem++;
return true;
}




